namespace ByteFarm.ImageMetaData.Format.Tiff
{
    public class ImageFileDirectoryFactory<TTag, TFieldType>
    {
        public ImageFileDirectoryFactory(DirectoryEntryFactory<TTag, TFieldType> directoryEntryFactory)
        {
            DirectoryEntryFactory = directoryEntryFactory;
        }

        public DirectoryEntryFactory<TTag, TFieldType> DirectoryEntryFactory { get; protected set; }


        public ImageFileDirectory<TTag, TFieldType> CreateImageFileDirectory(out int nextImageFileOffset)
        {
            int numDirectoryEntries = DirectoryEntryFactory.Reader.ReadUInt16();

            ImageFileDirectory<TTag, TFieldType> imageFileDirectory = new ImageFileDirectory<TTag, TFieldType>();
            for (int i = 0; i < numDirectoryEntries; i++)
            {
                DirectoryEntry<TTag, TFieldType> de = DirectoryEntryFactory.CreateDirectoryEntry();
                if (de != null)
                    imageFileDirectory.DirectoryEntries.Add(de);
            }


            nextImageFileOffset = (int) DirectoryEntryFactory.Reader.ReadUInt32();
            return imageFileDirectory;
        }
    }
}